package com.pzh.xzh.uaa.config;

import com.pzh.xzh.uaa.handler.PasswordHandler;
import com.pzh.xzh.uaa.service.imp.UserDetailServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @deprecated 单体security相关配置
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
     /* @Autowired
      private UserDetailsService userDetailsService;*/
     @Autowired
      private PasswordHandler passwordHandle;
     @Autowired
      private UserDetailServiceImp userDetailServiceImp;
      @Autowired
      private TokenStore tokenStore;
      @Bean
      public PasswordEncoder passwordEncoder(){
          return new BCryptPasswordEncoder();
      }
      //从父类来
      @Bean
      public AuthenticationManager authenticationManager() throws Exception {
          return super.authenticationManager();
      }

     /* @Bean
      public UserDetailsService userDetailsService(){
          InMemoryUserDetailsManager inMemoryUserDetailsManager=new InMemoryUserDetailsManager(

                  User.withUsername("admin").password(passwordEncoder().encode("admin")).authorities("salary").build()
          );
          return inMemoryUserDetailsManager;
          //return null;
      }*/


    /**
     * 安全策略
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        http.csrf().disable();
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(passwordHandle);
        auth.userDetailsService(userDetailServiceImp).passwordEncoder(new BCryptPasswordEncoder());
    }
}
